from collections import deque
info = input().split()
n = int(info[0])
q = int(info[1])
dq = deque(list(map(int, input().split())))
e_max = max(dq)
ans = list()
while True:
A = dq.popleft()
B = dq.popleft()
ans.append(str(A)+ ' ' + str(B))
if A > B:
dq.appendleft(A)
dq.append(B)
if A == e_max:
break
else:
dq.appendleft(B)
dq.append(A)
if B == e_max:
break
l = len(ans)
for i in range(q):
aux = int(input())
if aux <= l:
print(ans[aux - 1])
else:
pos = (aux - 1 - l) % (n - 1) + 1
print(str(e_max) + ' ' + str(dq[pos]))
#include <bits/stdc++.h>
#include <chrono>
#define all(x) (x).begin(), (x).end()
#define fin(s) freopen(s, "r", stdin);
#define fout(s) freopen(s, "w", stdout);
using namespace std;
typedef long long LL;
typedef long double LD;
int TN = 1;
void showDeque(deque<int> d) {
int n = d.size();
for (int i = 0; i < n; i++) {
cout << d.front() << " ";
d.push_back(d.front());
d.pop_front();
}
cout << endl << "==" << endl;
}
void solve() {
int n;
int q;
cin >> n >> q;
deque<int> d;
int maxValue = -1;
for (int i = 0; i < n; i++) {
int a_i;
cin >> a_i;
d.push_back(a_i);
maxValue = max(maxValue, a_i);
}
map<int, pair<int, int>> answer;
int maxIndex = 0;
while (true) {
/// showDeque(d);
int first = d.front();
d.pop_front();
int second = d.front();
d.pop_front();
if (first == maxValue) {
d.push_front(second);
d.push_front(first);
break;
}
maxIndex++;
answer[maxIndex] = {first, second};
if (second > first) {
swap(first, second);
}
d.push_front(first);
d.push_back(second);
}
int a[n];
/// showDeque(d);
for (int i = 0; i < n; i++) {
a[i] = d.front();
d.pop_front();
}
for (int i = 0; i < q; i++) {
LL m_j;
cin >> m_j;
if (m_j <= maxIndex) {
cout << answer[m_j].first << " " << answer[m_j].second << '\n';
} else {
cout << maxValue << " " << a[(m_j - (maxIndex + 1)) % (n - 1) + 1] << '\n';
}
}
}
int main() {
auto start = chrono::steady_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
/// =========================================
/// fin("input.txt"); fout("output.txt");
/// fin("file.in"); fout("file.out");
/// cin >> TN;
/// =========================================
while (TN--) solve();
auto finish = chrono::steady_clock::now();
auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(finish - start);
cerr << endl << "Time: " << elapsed_ms.count() << " ms\n";
return 0;
}
1323B - Count Subrectangles | 991C - Candies |
1463A - Dungeon | 1671D - Insert a Progression |
1671A - String Building | 1671B - Consecutive Points Segment |
1671C - Dolce Vita | 1669G - Fall Down |
4D - Mysterious Present | 1316B - String Modification |
1204A - BowWow and the Timetable | 508B - Anton and currency you all know |
1672A - Log Chopping | 300A - Array |
48D - Permutations | 677C - Vanya and Label |
1583B - Omkar and Heavenly Tree | 1703C - Cypher |
1511C - Yet Another Card Deck | 1698A - XOR Mixup |
1702E - Split Into Two Sets | 1703B - ICPC Balloons |
1702F - Equate Multisets | 1700A - Optimal Path |
665C - Simple Strings | 1708A - Difference Operations |
1703E - Mirror Grid | 1042A - Benches |
1676B - Equal Candies | 1705B - Mark the Dust Sweeper |